<template>
  <layout>
    <!-- 查看文件上传内容 -->
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索土地出让方</a-button>
        
      </custom-form>
    </section>

    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="record => record.field_id"
      :pagination="false"
      :loading="tableLoading"
      :customRow="customTableRow"
      size='small'
      :scroll="{
        x: '100%',
        y: 520
      }"
      bordered
    >
      <template
        slot="field_type"
        slot-scope="row"
      >
        {{getLabel(row.field_type, [], 'field_type')}}
      </template>

      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-popover title="请输入转入面积" :visible="row.popoverVisible">
          <template slot="content">
            <a-input v-model="currentSelectArea">
              <template slot="addonAfter">
                <a-button
                  class="action-button"
                  size="small"
                  type="link"
                  :loading="intoLoading"
                  @click="handlerSureInto(row)"
                >确定</a-button>
              </template>
            </a-input>
          </template>
          <a-button
            class="action-button"
            type="link"
            size="small"
            @click="handlerShowIntoInput(row)"
          >转入到该农户</a-button>
        </a-popover>
      </template>
      
    </a-table>

    <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      :show-total="showTotal"
      showSizeChanger
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination>
  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { famerServices, IIntoField } from '@/common/services/famer'
import { publicServices } from '@/common/services/public'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: String
  })
  public currentAreaId!: string
  @Prop({
    type: String
  })
  public currentMemberId!: string
  @Prop({
    type: String
  })
  public currentAreaName!: string

  public commonParams = {
    s_a_id: ''
  }

  // 产品列表搜索条件
  public tableSearch = {
    type: '1',
    family_id: ''
  }

  // 当前面积
  public currentSelectArea = ''
  public popoverVisible = false

  // 自定义mode
  public customMode = 'search'

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '用户类型',
      dataIndex: 'type',
      key: 'type',
      width: 100,
      form: {
        type: 'a-radio-group',
        isShowQuery: true,
        isShowList: false,
        dict: [
          {
            label: '农户',
            value: '1'
          },
          {
            label: '大户',
            value: '2'
          }
        ]
      }
    },
    {
      title: '耕地地址所属行政区',
      dataIndex: 's_a_id',
      key: 's_a_id',
      width: 140,
      form: {
        type: 'a-tree-select',
        // treeDataSimpleMode: true,
        treeData: [
          {
            s_a_id: this.currentAreaId,
            s_a_nam: this.currentAreaName
          }
        ],
        // showSearch: true,
        treeNodeFilterProp: 'title',
        loadData: this.onLoadData,
        dropdownStyle: {
          'overflow-y': 'scroll',
          maxHeight: "600px"
        },
        replaceFields: { title: 's_a_nam', key: 's_a_id', value: 's_a_id', children: 'children' },
        placeholder: '请选择耕地地址所属行政区',
        isShowQuery: true,
        isShowList: false
      }
    },
    
    {
      title: '耕地编号',
      dataIndex: 'field_id',
      key: 'field_id',
      width: 100,
      form: {
        type: 'a-input',
        placeholder: '请输入耕地编号'
      }
    },
    {
      title: '农户/大户编号',
      dataIndex: 'member_id',
      key: 'member_id',
      width: 140,
      form: {
        type: 'a-input',
        placeholder: '请输入农户/大户编号',
      }
    },
    {
      title: '身份证/法定编码',
      dataIndex: 'sfz',
      key: 'sfz',
      width: 150,
      form: {
        type: 'a-input',
        placeholder: '请输入身份证/法定编码'
      }
    },
    {
      title: '编号/姓名',
      dataIndex: 'key',
      key: 'key',
      width: 110,
      form: {
        type: 'a-input',
        placeholder: '请输入关键字',
        isShowQuery: true,
        isShowList: false
      }
    },

    {
      title: '农户/大户姓名',
      dataIndex: 'member_nam',
      key: 'member_nam',
      width: 180,
      form: {
        type: 'a-input',
        placeholder: '请输入农户/大户姓名',
      }
    },
    
    {
      title: '耕地地址',
      dataIndex: 's_a_nam_all',
      key: 's_a_nam_all',
      width: 150
    },
    {
      title: '耕地类型',
      key: 'field_type',
      scopedSlots: { customRender: 'field_type' },
      width: 80,
      form: {
         type: 'a-radio-group',
          dict: [
            {
              label: '田',
              value: '1'
            },
            {
              label: '土',
              value: '2'
            }
          ]
      }
    },
    {
      title: '确权面积（亩）',
      dataIndex: 'field_size',
      key: 'field_size',
      width: 110
    },
    // {
    //   title: '备注',
    //   dataIndex: 'readme',
    //   key: 'readme',
    //   width: 110
    // },
    {
      title: '操作',
      key: 'action',
      width: 100,
      scopedSlots: { customRender: 'action' }
    }
  ]

  public tableApi = {
    list: famerServices.getIntoFieldsList
  }

  @Watch('currentAreaId')
  public handlerChangeCurrentAreaId() {
    this.commonParams.s_a_id = this.currentAreaId
    this.tableSearch.family_id = this.currentMemberId
    this.getData()
  }

  public mounted() {
    this.commonParams.s_a_id = this.currentAreaId
    this.tableSearch.family_id = this.currentMemberId
    // this.tableData = this.execlData || []
  }

  public async getData() {
    const res = await this.getTableList()
  }

  public intoLoading = false
  public handlerSureInto(row: IIntoField) {
    if (+this.currentSelectArea <= 0 || +this.currentSelectArea > row.field_size) {
      this.$message.error('转入面积错误！请重新输入')
      return
    }
    this.intoLoading = true
    famerServices.updateIntoField({
      field_id: row.field_id,
      member_id: row.member_id,
      field_size: +this.currentSelectArea,
      family_id: this.currentMemberId,
      type: this.tableSearch.type
    }).then(_ => {
      this.$message.success('转入成功')
      // @ts-ignore
      row.popoverVisible = false
      this.getData()
    }).finally(() => {
      this.intoLoading = false
    })
  }

  // 显示转地输入框
  public handlerShowIntoInput(row: IIntoField & { popoverVisible: boolean }) {
    this.currentSelectArea = ''
    this.$set(row, 'popoverVisible', row.popoverVisible ? !row.popoverVisible : true)
  }

  public async onLoadData(treeNode?: any) {
    const res = await publicServices.getAreaLazy({
      s_a_id: treeNode ? treeNode.dataRef.s_a_id : this.currentAreaId || '0'
    })
    // 判断是否是叶子节点
    const treeData = res.data.map(item => {
      if (item.s_a_id.length === 14) {
        // @ts-ignore
        item.isLeaf = true
      }
      return item
    })
    
    if (!treeNode) {
      this.changeDict('s_a_id', treeData, 'treeData')
    } else {
      treeNode.dataRef.children = treeData
      const currentTreeData = this.columns.find((column) => {
        return 's_a_id' === column.key && column.form;
      })
      this.changeDict('s_a_id', [...(currentTreeData?.form?.treeData || [])], 'treeData')
    }
  }
}
</script>

<style scoped lang="less">
.action-button {
  margin: 0px 10px 0px 10px;
}
.upload-button {
  position: relative;
  input {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    z-index: 2;
    opacity: 0;
  }
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
